package cn.hnu.qq.client.panel;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.log4j.Logger;

import cn.hnu.qq.client.controller.ListenServerMessageThread;
import cn.hnu.qq.client.controller.SearchController;
import cn.hnu.qq.client.event.MessageEvent;
import cn.hnu.qq.client.listener.MessageListener;
import cn.hnu.qq.entity.Crowd;
import cn.hnu.qq.entity.message.Message;
import cn.hnu.qq.entity.message.ResponseSearchMessage;
import cn.hnu.qq.global.MessageType;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;
import javax.swing.ImageIcon;

public class SearchCrowdPanel extends JPanel implements MessageListener,ActionListener{
	private JTextField textField;

	private JLabel crowdName;
	
	private JLabel creater;
	
	private SearchController controller = SearchController.getInstance();
	/**
	 * Create the panel.
	 */
	public SearchCrowdPanel() {
		setLayout(null);
		
		JPanel panel = new JPanel(){
			@Override
			protected void paintComponent(Graphics g) {
				Image image = null;
				try {
					image = ImageIO.read(new File("src/main/java/image/02.jpg"));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				};
						
				g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);

			}
		};
		panel.setBounds(10, 10, 319, 80);
		add(panel);
		panel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel lblCrowdName = new JLabel("crowd name:");
		panel.add(lblCrowdName, "4, 4");
		
		textField = new JTextField();
		panel.add(textField, "6, 4, left, default");
		textField.setColumns(15);
		
		JButton btnNewButton = new JButton("search");
		panel.add(btnNewButton, "8, 4, center, default");
		btnNewButton.setActionCommand("search");
		btnNewButton.addActionListener(this);
		JPanel panel_1 = new JPanel(){
			@Override
			protected void paintComponent(Graphics g) {
				Image image = null;
				try {
					image = ImageIO.read(new File("src/main/java/image/02.jpg"));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				};
						
				g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);

			}
		};
		panel_1.setBounds(10, 100, 319, 101);
		add(panel_1);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(SearchCrowdPanel.class.getResource("/image/xiaohui2.png")));
		panel_1.add(lblNewLabel, BorderLayout.WEST);
		
		JPanel panel_2 = new JPanel(){
			@Override
			protected void paintComponent(Graphics g) {
				Image image = null;
				try {
					image = ImageIO.read(new File("src/main/java/image/02.jpg"));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				};
						
				g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);

			}
		};
		panel_1.add(panel_2, BorderLayout.CENTER);
		panel_2.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel lblCrowdName_1 = new JLabel("crowd name:");
		panel_2.add(lblCrowdName_1, "4, 4");
		
		crowdName = new JLabel("");
		panel_2.add(crowdName, "8, 4");
		
		JLabel lblCreater = new JLabel("creater:");
		panel_2.add(lblCreater, "4, 6");
		
		creater = new JLabel("");
		panel_2.add(creater, "8, 6");
		ListenServerMessageThread.getInstance().addMessageListener(this);
	}
	@Override
	protected void paintComponent(Graphics g) {
		Image image = null;
		try {
			image = ImageIO.read(new File("src/main/java/image/05.jpg"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		};
				
		g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);

	}
	public String getCrowdName() {
		return crowdName.getText();
	}
	public void setCrowdName(String crowdName) {
		this.crowdName.setText(crowdName);
		this.crowdName.validate();
	}
	public String getCreater() {
		return creater.getText();
	}
	public void setCreater(String creater) {
		this.creater.setText(creater);
		this.creater.validate();
	}
	public void messageArrived(MessageEvent event) {
		Message message = event.getSource();
		
		if(message.getMessageType() == MessageType.SEARCH_CROWD){
			ResponseSearchMessage rsp = (ResponseSearchMessage) message;
			Crowd crowd = (Crowd) rsp.getScource();
			if(crowd == null){
				return;
			}
			setCrowdName(crowd.getCrowdName());
			setCreater(crowd.getCreater().getAcount());
		}
		
	}
	public String getSearchName() {
		return this.textField.getText();
	}
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("search")){
			String crowdName = getSearchName();
			if(crowdName == null || crowdName.equals("")){
				JOptionPane.showMessageDialog(this, "请输入群组号！！");
				return ;
			}
			controller.searchCrowd(crowdName);
			return;
		}
	}
	public String getSearchedName() {
		return getCrowdName();
	}
	
	
}
